<?php
namespace Home\Controller;
use Think\Controller;
class AdminController extends Controller {

    //后台首页
    public function index(){
        $loginController = A('Login');
        $loginStatus = $loginController->checkLogin(cookie('loginSession'));
        $this->assign('loginStatus',$loginStatus);
        $this->assign('page','index');
        $this->display();
    }
    //wow统计页面
    public function wow(){
        $loginController = A('Login');
        $loginStatus = $loginController->checkLogin(cookie('loginSession'));
        $this->assign('loginStatus',$loginStatus);
        $this->assign('page','wow');
        $Form = M("wow_static");
        $data = $Form->field('realm')->group('realm')->select();
        $this->assign('realms',$data);
        $this->display();
    }
    public function getStatByRealmAndMonth($realm,$month){
        $Form = M("wow_static");
        $where = 'realm="'. $realm .'" AND month="'. $month .'"';
        if($realm == '') $where = 'month="'. $month .'"';
        $data = $Form->where($where)->select();
        $this->ajaxReturn($data);
    }
    //文档编辑页
    public function doc(){
        $loginController = A('Login');
        $loginStatus = $loginController->checkLogin(cookie('loginSession'));
        $this->assign('loginStatus',$loginStatus);
        $this->assign('page','doc');
        $this->assign('URL_PREFIX',C("URL_PREFIX"));
        $this->display();
    }
    //发布blog
    public function publishBlog($id,$title,$tag,$abstract,$content){
        $loginController = A('Login');
        $loginStatus = $loginController->checkLogin(cookie('loginSession'));
        $Form   =   D('blog');
        //新增
        if($Form->create() && $title != "" && $tag != "" && $content != "" && $loginStatus == 1 && $id == -1) {
                    $Form->title = $title;
                    $Form->tag = $tag;
                    if($abstract == "" || $abstract == null) $abstract = $title;
                    $Form->abstract = $abstract;
                    $Form->content = $content;
                    $Form->create_time = date("Y-m-d H:i:s" ,time());
                    $result = $Form->add();
        }
        //修改
        else if($title != "" && $tag != "" && $content != "" && $loginStatus == 1 && $id > -1){
                $blog = M("blog");
                $blog->title = $title;
                $blog->tag = $tag;
                if($abstract == "" || $abstract == null) $abstract = $title;
                $blog->abstract = $abstract;
                $blog->content = $content;
                $result= $blog->where('id = '.$id)->save();
        }else{
             $this->error($Form->getError());
        }
        //判断是否新增或修改成功;
        if($result) {
            S("monthSession",null);
            S("tagCountSession",null);
            $this->success('Success');
        }else{
            $this->error('Error');
        }
    }
    //编辑blog
    public function editBlog($id){
        $loginController = A('Login');
        $loginStatus = $loginController->checkLogin(cookie('loginSession'));
        $Form = M("blog");
        if($loginStatus == 1){
            $data = $Form -> where('id ='.$id)->find();
            $this->assign('data',$data);
            $this->assign('loginStatus',$loginStatus);
            $this->assign('page','doc');
            $this->assign('URL_PREFIX',C("URL_PREFIX"));
            $this->display(T('Admin/doc'));
        }
    }
}